package com.young.cms.model;

import com.young.cms.model.enums.EAsConditionTypeEnum;
import com.young.cms.model.enums.EColumnTypeEnum;
import com.young.cms.model.enums.EControlTypeEnum;
import com.young.cms.model.enums.EShowFormatEnum;
import com.young.common.core.excel.ExcelColumn;
import com.young.common.model.BaseDO;
import com.young.common.model.enums.EYesOrNoEnum;
import com.young.common.util.StringUtils;


/**
 * 代码生成字段配置实体类
 * Created by rookie on 2018-7-12.
 */
public class GenCol {

    @ExcelColumn(value = "主键", order = 1)
    private String id;//主键

    @ExcelColumn(value = "代码生成编号,关联tb_cms_gen_main", order = 2)
    private String genId;//代码生成编号,关联tb_cms_gen_main

    @ExcelColumn(value = "字段名", order = 3)
    private String fieldName;//字段名

    @ExcelColumn(value = "java属性名", order = 4)
    private String attrName;//java属性名

    @ExcelColumn(value = "字段描述", order = 5)
    private String fieldRemark;//字段描述

    @ExcelColumn(value = "字段类型", order = 6)
    private String fieldType;//字段类型

    @ExcelColumn(value = "字段长度", order = 7)
    private Integer fieldLength;//字段长度

    @ExcelColumn(value = "字段非空", order = 8)
    private String fieldNotNull;//字段非空

    @ExcelColumn(value = "是否在视图显示", order = 9)
    private String viewShow;//是否在视图显示

    @ExcelColumn(value = "在视图中的顺序", order = 10)
    private Integer viewNum;//在视图中的顺序

    @ExcelColumn(value = "是否作为查询条件", order = 11)
    private String asCondition;//是否作为查询条件

    @ExcelColumn(value = "是否支持排序", order = 12)
    private String supportSort;//是否支持排序

    @ExcelColumn(value = "在表单显示方式", order = 13)
    private String formShow;//在表单显示方式

    @ExcelColumn(value = "在表单中的顺序", order = 14)
    private Integer formNum;//在表单中的顺序

    @ExcelColumn(value = "控件类型,input/select..", order = 15)
    private String controlType;//控件类型,input/select..

    @ExcelColumn(value = "是否必填", order = 16)
    private String controlMust;//是否必填

    @ExcelColumn(value = "控件参数,例如url,取值等", order = 17)
    private String controlParam;//控件参数,例如url,取值等

    @ExcelColumn(value = "默认值", order = 18)
    private String controlDefault;//默认值

    @ExcelColumn(value = "只读属性", order = 19)
    private String controlReadonly;//只读属性

    @ExcelColumn(value = "控件样式", order = 20)
    private String controlClass;//控件样式

    @ExcelColumn(value = "控件表单验证", order = 21)
    private String controlCheck;//控件表单验证

    @ExcelColumn(value = "控件表单验证值", order = 22)
    private String controlCheckVal;//控件表单验证值

    @ExcelColumn(value = "填写说明", order = 23)
    private String fillExplain;//填写说明



    // ===== 代码生成V4新增字段 start =====

    // 字段java类型
    private String javaType;
    // 支持导入
    private String supportImport;
    // 支持导出
    private String supportExport;

    // 表头标题
    private String headerTitle;
    // 表头align, left,center,right
    private String headerAlign;
    // 内容align, left,center,right
    private String valueAlign;
    // 固定列, left,no,right
    private String columnFixed;
    // 表头提示
    private String headerTips;
    // 列宽
    private String columnWidth;
    // 视图格式
    private String viewValueFormat;

    // 表单控件宽度
    private Integer controlWidth;
    // 控件配置
    private String controlConfig;
    // 显示控制
    private String showConfig;


    // ===== 代码生成V4新增字段 end =====

    public GenCol() {
        super();
    }

//    public static GenCol buildDefaultV4(Column column) {
//        GenCol genCol = new GenCol();
////        genCol.setId();
////        genCol.setGenId();
//        genCol.setFieldName(column.getField());
//        genCol.setAttrName(StringUtils.toCamel(column.getField()));
//        genCol.setFieldRemark(column.getRemark());
//        genCol.setFieldType(column.getType());
//        genCol.setFieldLength(column.getLength());
//        genCol.setFieldNotNull(column.getNotNull());
//
//        // 字段类型
//        EColumnTypeEnum columnTypeEnum = EColumnTypeEnum.of(column.getType());
//
//        // 字段
//        genCol.setJavaType(columnTypeEnum != null ? columnTypeEnum.getJavaType() : "String");
//
//
//        // 视图
//        if ("text".equalsIgnoreCase(column.getType())
//                || (column.getLength() != null && column.getLength() > 2000) ) {
//            genCol.setViewShow("0");
//            genCol.setSupportImport(EYesOrNoEnum.NO.getCode().toString());
//            genCol.setSupportExport(EYesOrNoEnum.NO.getCode().toString());
//        } else {
//            genCol.setViewShow("1");
//            genCol.setSupportImport(EYesOrNoEnum.YES.getCode().toString());
//            genCol.setSupportExport(EYesOrNoEnum.YES.getCode().toString());
//        }
//        genCol.setViewNum(9000);
//
//        if (columnTypeEnum != null && (columnTypeEnum.isNumberType() || columnTypeEnum.isDateType())) {
//            genCol.setSupportSort("1");
//        } else {
//            genCol.setSupportSort("0");
//        }
//        if (column.getField().toLowerCase().endsWith("id")) {
//            genCol.setAsCondition(EAsConditionTypeEnum.EQ.getType());
//        } else if (column.getField().toLowerCase().endsWith("name")) {
//            genCol.setAsCondition(EAsConditionTypeEnum.LIKE.getType());
//        } else if (column.getField().toLowerCase().endsWith("time")) {
//            genCol.setAsCondition(EAsConditionTypeEnum.BETWEEN_AND.getType());
//        } else {
//            genCol.setAsCondition(EAsConditionTypeEnum.NO.getType());
//        }
//        if (column.getRemark().split(",").length > 1) {
//            genCol.setHeaderTitle(column.getRemark().split(",")[0]);
//        } else {
//            genCol.setHeaderTitle(column.getRemark());
//        }
//        genCol.setHeaderAlign("left");
//        genCol.setValueAlign("left");
//        genCol.setColumnFixed("no");
//        genCol.setHeaderTips("");
//        genCol.setColumnWidth("100");
//        genCol.setViewValueFormat(EShowFormatEnum.TEXT.getType());
//
//        // 表单
//        genCol.setFormShow("1");
//        genCol.setFormNum(9000);
//        genCol.setControlType(EControlTypeEnum.INPUT.getType());
//        if (EYesOrNoEnum.YES.getCode().toString().equals(column.getNotNull())) {
//            genCol.setControlMust(EYesOrNoEnum.YES.getCode().toString());
//        } else {
//            genCol.setControlMust(EYesOrNoEnum.NO.getCode().toString());
//        }
////        genCol.setControlParam();
////        genCol.setControlDefault();
//        genCol.setControlReadonly("none");
//        genCol.setControlClass("2");
////        genCol.setControlCheck();
////        genCol.setControlCheckVal();
////        genCol.setFillExplain();
//
//        genCol.setControlWidth(12);
////        genCol.setControlConfig();
////        genCol.setShowConfig();
//        return genCol;
//    }

    /**
     * 构造默认col对象
     * @param column
     * @return
     */
    public static GenCol buildDefault(Column column) {
        GenCol genCol = new GenCol();
//        genCol.setId();
//        genCol.setGenId();
        genCol.setFieldName(column.getField());
        genCol.setAttrName(StringUtils.toCamel(column.getField()));
        genCol.setFieldRemark(column.getRemark());
        genCol.setFieldType(column.getType());
        genCol.setFieldLength(column.getLength());
        genCol.setFieldNotNull(column.getNotNull());
        genCol.setViewShow("1");
        genCol.setViewNum(9000);
        genCol.setAsCondition("1");
        genCol.setSupportSort("0");
        genCol.setFormShow("1");
        genCol.setFormNum(9000);
//        genCol.setControlType("input");
        genCol.setControlMust("0");
//        genCol.setControlParam();
//        genCol.setControlDefault();
        genCol.setControlReadonly("none");
        genCol.setControlClass("2");
//        genCol.setControlCheck();
//        genCol.setControlCheckVal();
//        genCol.setFillExplain();
        return genCol;
    }

    /**
     * 判断是否BaseDO中已有的字段
     *
     * @return
     */
    public boolean isBaseDOField() {
        for (String field : BaseDO.allField()) {
            if (field.equals(this.attrName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 将字段类型转换为对应的java类型
     */
    public void convertFieldJavaType() {
        if (this.getFieldType().indexOf("char") > -1 || this.getFieldType().indexOf("CHAR") > -1
                || this.getFieldType().indexOf("text") > -1 || this.getFieldType().indexOf("TEXT") > -1) {
            this.setFieldType("String");
        } else if (this.getFieldType().indexOf("date") > -1 || this.getFieldType().indexOf("DATE") > -1
                || this.getFieldType().indexOf("time") > -1 || this.getFieldType().indexOf("TIME") > -1) {
            this.setFieldType("Date");
        } else if ("bigint".equalsIgnoreCase(this.getFieldType())) {
            this.setFieldType("Long");
        } else if (this.getFieldType().indexOf("number") > -1 || this.getFieldType().indexOf("NUMBER") > -1
                || this.getFieldType().indexOf("int") > -1 || this.getFieldType().indexOf("INT") > -1) {
            this.setFieldType("Integer");
        } else if (this.getFieldType().indexOf("float") > -1 || this.getFieldType().indexOf("FLOAT") > -1) {
            this.setFieldType("Float");
        } else if (this.getFieldType().indexOf("double") > -1 || this.getFieldType().indexOf("DOUBLE") > -1) {
            this.setFieldType("Double");
        } else {
            this.setFieldType("String");
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getGenId() {
        return genId;
    }

    public void setGenId(String genId) {
        this.genId = genId;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getAttrName() {
        return attrName;
    }

    public void setAttrName(String attrName) {
        this.attrName = attrName;
    }

    public String getFieldRemark() {
        return fieldRemark;
    }

    public void setFieldRemark(String fieldRemark) {
        this.fieldRemark = fieldRemark;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public Integer getFieldLength() {
        return fieldLength;
    }

    public void setFieldLength(Integer fieldLength) {
        this.fieldLength = fieldLength;
    }

    public String getFieldNotNull() {
        return fieldNotNull;
    }

    public void setFieldNotNull(String fieldNotNull) {
        this.fieldNotNull = fieldNotNull;
    }

    public String getViewShow() {
        return viewShow;
    }

    public void setViewShow(String viewShow) {
        this.viewShow = viewShow;
    }

    public Integer getViewNum() {
        return viewNum;
    }

    public void setViewNum(Integer viewNum) {
        this.viewNum = viewNum;
    }

    public String getAsCondition() {
        return asCondition;
    }

    public void setAsCondition(String asCondition) {
        this.asCondition = asCondition;
    }

    public String getSupportSort() {
        return supportSort;
    }

    public void setSupportSort(String supportSort) {
        this.supportSort = supportSort;
    }

    public String getFormShow() {
        return formShow;
    }

    public void setFormShow(String formShow) {
        this.formShow = formShow;
    }

    public Integer getFormNum() {
        return formNum;
    }

    public void setFormNum(Integer formNum) {
        this.formNum = formNum;
    }

    public String getControlType() {
        return controlType;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    public String getControlMust() {
        return controlMust;
    }

    public void setControlMust(String controlMust) {
        this.controlMust = controlMust;
    }

    public String getControlParam() {
        return controlParam;
    }

    public void setControlParam(String controlParam) {
        this.controlParam = controlParam;
    }

    public String getControlDefault() {
        return controlDefault;
    }

    public void setControlDefault(String controlDefault) {
        this.controlDefault = controlDefault;
    }

    public String getControlReadonly() {
        return controlReadonly;
    }

    public void setControlReadonly(String controlReadonly) {
        this.controlReadonly = controlReadonly;
    }

    public String getControlClass() {
        return controlClass;
    }

    public void setControlClass(String controlClass) {
        this.controlClass = controlClass;
    }

    public String getControlCheck() {
        return controlCheck;
    }

    public void setControlCheck(String controlCheck) {
        this.controlCheck = controlCheck;
    }

    public String getControlCheckVal() {
        return controlCheckVal;
    }

    public void setControlCheckVal(String controlCheckVal) {
        this.controlCheckVal = controlCheckVal;
    }

    public String getFillExplain() {
        return fillExplain;
    }

    public void setFillExplain(String fillExplain) {
        this.fillExplain = fillExplain;
    }

    public String getHeaderTitle() {
        return headerTitle;
    }

    public void setHeaderTitle(String headerTitle) {
        this.headerTitle = headerTitle;
    }

    public String getHeaderAlign() {
        return headerAlign;
    }

    public void setHeaderAlign(String headerAlign) {
        this.headerAlign = headerAlign;
    }

    public String getValueAlign() {
        return valueAlign;
    }

    public void setValueAlign(String valueAlign) {
        this.valueAlign = valueAlign;
    }

    public String getColumnFixed() {
        return columnFixed;
    }

    public void setColumnFixed(String columnFixed) {
        this.columnFixed = columnFixed;
    }

    public String getHeaderTips() {
        return headerTips;
    }

    public void setHeaderTips(String headerTips) {
        this.headerTips = headerTips;
    }

    public String getColumnWidth() {
        return columnWidth;
    }

    public void setColumnWidth(String columnWidth) {
        this.columnWidth = columnWidth;
    }

    public String getViewValueFormat() {
        return viewValueFormat;
    }

    public void setViewValueFormat(String viewValueFormat) {
        this.viewValueFormat = viewValueFormat;
    }

    public String getJavaType() {
        return javaType;
    }

    public void setJavaType(String javaType) {
        this.javaType = javaType;
    }

    public String getSupportImport() {
        return supportImport;
    }

    public void setSupportImport(String supportImport) {
        this.supportImport = supportImport;
    }

    public String getSupportExport() {
        return supportExport;
    }

    public void setSupportExport(String supportExport) {
        this.supportExport = supportExport;
    }

    public Integer getControlWidth() {
        return controlWidth;
    }

    public void setControlWidth(Integer controlWidth) {
        this.controlWidth = controlWidth;
    }

    public String getControlConfig() {
        return controlConfig;
    }

    public void setControlConfig(String controlConfig) {
        this.controlConfig = controlConfig;
    }

    public String getShowConfig() {
        return showConfig;
    }

    public void setShowConfig(String showConfig) {
        this.showConfig = showConfig;
    }
}